package jmine.tec.report.cell.impl;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.NumberFormat;

import jmine.tec.report.cell.DataCell;
import jmine.tec.report.cell.FormattedCell;

/**
 * Represents a percentage. Differs from double because it multiplies the number by 100 and add '%' while formatting
 * 
 * @author takeshi
 */
public class PercentageDataCell extends AbstractDoubleValueDataCell implements DataCell, Serializable, FormattedCell {

    private static final double NUM_100 = 100.0;

    private static final long serialVersionUID = 1L;

    private final NumberFormat nf;

    /**
     * Construtor. Recebe o n�mero que ser� mostrado em percentagem.
     * 
     * @param number {@link Double} n�mero que ser� mostrado em percentagem
     */
    public PercentageDataCell(final Double number) {
        super(number);
        this.nf = null;
    }

    /**
     * C'tor with primitive
     * 
     * @param value the value
     */
    public PercentageDataCell(final double value) {
        this(Double.valueOf(value));
    }

    /**
     * C'tor
     * 
     * @param value o valor
     * @param nf o {@link NumberFormat}
     */
    public PercentageDataCell(double value, NumberFormat nf) {
        super(value);
        this.nf = nf;
    }

    /**
     * Formats this cell as a String
     * 
     * @param dateFormat {@link DateFormat}
     * @param doubleFormat {@link NumberFormat}
     * @param currencyFormat {@link NumberFormat}
     * @return {@link String}
     */
    public String toString(final DateFormat dateFormat, final NumberFormat doubleFormat, final NumberFormat currencyFormat) {
        Double value = this.toDouble();
        if (value != null) {
            return doubleFormat.format(value * NUM_100) + "%";
        }
        return "";
    }

    /**
     * {@inheritDoc}
     */
    public String format() {
        if (this.nf == null) {
            return this.toString();
        }
        return this.nf.format(this.toDouble().doubleValue() * NUM_100) + "%";
    }

    /**
     * {@inheritDoc}
     */
    public NumberFormat getNumberFormat() {
        return this.nf;
    }

}
